package org.jpass.dc.business.project.config;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class ConfigReader {
	
	private Log log = LogFactory.getLog(ConfigReader.class);
	
	private Document doc;
	
	/*private static ConfigReader instance = new ConfigReader();
	
	public static ConfigReader getInstance() {
		return instance;
	}*/
	
	public ConfigReader() {}
	
	public ConfigReader(String resource) {
		try {
			doc = new SAXReader().read(Thread.currentThread()
					.getContextClassLoader().getResourceAsStream(resource));
		} catch (DocumentException e) {
			e.printStackTrace();
			log.error("获取" + resource + "文件的dom对象时出现异常，异常信息为：", e);
		}
	}
	
	public Document getDomObj(String resource) {
		Document doc = null;
		try {
			doc = new SAXReader().read(Thread.currentThread()
					.getContextClassLoader().getResourceAsStream(resource));
		} catch (DocumentException e) {
			log.error("获取" + resource + "文件的dom对象时出现异常，异常信息为：", e);
		}
		
		return doc;
	}
	
	public Object getElementValue(String element) {
		return (Element)doc.selectObject(element);
	}
	
	@SuppressWarnings("unchecked")
	public List<Node> getElementSValue(String element) {
		return doc.selectNodes(element);
	}
}
